package com.xdhl.fu.fi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.utils.bean.BeanUtils;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.core.web.page.TableDataInfo;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.fi.repository.MGFlightAirportInfoRepository;
import com.xdhl.fu.fi.runner.LoadAirportsHotToCacheAndESRunner;
import com.xdhl.fu.fi.service.IFlightAirportInfoService;
import com.xdhl.fu.flight.info.api.domain.FlightAirportInfo;
import com.xdhl.fu.flight.info.api.mongodb.domain.MGFlightAirportInfo;
import org.springframework.data.domain.Example;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 机场信息controller
 *
 * @author 朱力维
 * @time 2022/6/3 19:47
 */
@RestController
@RequestMapping("/airport-info")
public class FlightAirportInfoController extends BaseController {
    @Resource
    private IFlightAirportInfoService flightAirportInfoService;

    @Resource
    private MGFlightAirportInfoRepository flightAirportInfoRepository;

    @Resource
    private LoadAirportsHotToCacheAndESRunner loadAirportsHotToCacheAndESRunner;

    /**
     * 获取城市信息
     *
     * @param titleWorld 城市拼音首字母
     * @param hot        是否热门城市
     * @return 城市列表
     */
    @GetMapping("/cities")
    public AjaxResult cities(@RequestParam(value = "titleWorld", required = false) String titleWorld, @RequestParam("isHot") String hot) {
        MGFlightAirportInfo mgFlightAirportInfo = new MGFlightAirportInfo();
        if ("1".equals(hot)) {
            mgFlightAirportInfo.setIsHot(hot);
        } else {
            mgFlightAirportInfo.setTitleWord(titleWorld);
        }
        Example<MGFlightAirportInfo> example = Example.of(mgFlightAirportInfo);
        List<MGFlightAirportInfo> list = flightAirportInfoRepository.findAll(example);
        return AjaxResult.success(list);
    }

    /**
     * 按关键字搜索机场信息
     *
     * @param matchWords 关键字
     * @return 符合条件的航班信息
     */
    @RequiresPermissions("flight:airport-info:list")
    @GetMapping("/list/match-words/{matchWords}")
    public AjaxResult listByMatchWords(@PathVariable("matchWords") String matchWords) {
        List<MGFlightAirportInfo> mgFlightAirportInfos = flightAirportInfoRepository.findByMatchWordsLike(matchWords);
        return AjaxResult.success(mgFlightAirportInfos);
    }

    /**
     * 按城市名搜索机场信息
     *
     * @param cityName 关键字
     * @return 符合条件的航班信息
     */
    @RequiresPermissions("flight:airport-info:list")
    @GetMapping("/list/city-name/{cityName}")
    public AjaxResult listByCityName(@PathVariable(name = "cityName", required = false) String cityName) {
        LambdaQueryWrapper<FlightAirportInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(!"null".equals(cityName), FlightAirportInfo::getCityName, cityName);
        List<FlightAirportInfo> flightAirportInfos = flightAirportInfoService.list(wrapper);

        return AjaxResult.success(flightAirportInfos);
    }

    /**
     * 查询热门城市
     */
    @RequiresPermissions("flight:airport-info:list")
    @GetMapping("/list/hot")
    public AjaxResult list() {
        LambdaQueryWrapper<FlightAirportInfo> condition = new LambdaQueryWrapper<>();
        condition.eq(FlightAirportInfo::getIsHot, "1");
        List<FlightAirportInfo> list = flightAirportInfoService.list(condition);
        return AjaxResult.success(list);
    }

    /**
     * 查询机场信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FlightAirportInfo flightAirportInfo) {
        startPage();
        LambdaQueryWrapper<FlightAirportInfo> condition = new LambdaQueryWrapper<>();
        List<FlightAirportInfo> list = flightAirportInfoService.list(condition);
        return getDataTable(list);
    }

    /**
     * 获取机场信息详细信息
     */
    @RequiresPermissions("flight:airport-info:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResult.success(flightAirportInfoService.getById(dataId));
    }

    /**
     * 新增机场信息
     */
    @RequiresPermissions("flight:airport-info:add")
    @PostMapping
    public AjaxResult add(@RequestBody FlightAirportInfo flightAirportInfo) {
        return toAjax(flightAirportInfoService.save(flightAirportInfo));
    }

    /**
     * 修改热门城市
     */
    @Transactional
    @RequiresPermissions("flight:airport-info:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody List<FlightAirportInfo> flightAirportInfos) throws Exception {
        if (flightAirportInfos.size() > 16) {
            return AjaxResult.error("热门城市个数不能大于16。");
        }
        // 移除原有的热门城市
        LambdaQueryWrapper<FlightAirportInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlightAirportInfo::getIsHot, "1");
        List<FlightAirportInfo> sourceHotList = flightAirportInfoService.list(wrapper);
        for (FlightAirportInfo flightAirportInfo : sourceHotList) {
            flightAirportInfo.setIsHot("0");
            flightAirportInfoService.updateById(flightAirportInfo);
            flightAirportInfoRepository.deleteById(flightAirportInfo.getDataId());
            MGFlightAirportInfo mgFlightAirportInfo = new MGFlightAirportInfo();
            BeanUtils.copyBeanProp(mgFlightAirportInfo, flightAirportInfo);
            flightAirportInfoRepository.save(mgFlightAirportInfo);
        }
        // 新增热门城市
        for (FlightAirportInfo item : flightAirportInfos) {
            FlightAirportInfo temp = flightAirportInfoService.getById(item.getDataId());
            temp.setIsHot("1");
            flightAirportInfoService.updateById(temp);
            MGFlightAirportInfo mgFlightAirportInfo = new MGFlightAirportInfo();
            BeanUtils.copyBeanProp(mgFlightAirportInfo, temp);
            flightAirportInfoRepository.save(mgFlightAirportInfo);
        }
        // 更新缓存
        loadAirportsHotToCacheAndESRunner.run();
        return AjaxResult.success("修改成功。");
    }

    /**
     * 删除机场信息
     */
    @RequiresPermissions("flight:airport-info:remove")
    @DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable String[] dataIds) {
        return toAjax(flightAirportInfoService.removeByIds(Arrays.asList(dataIds)));
    }

}
